package com.shiyanlou.upms.system.service.impl;

import com.shiyanlou.upms.common.domain.Tree;
import com.shiyanlou.upms.common.utils.BuildTree;
import com.shiyanlou.upms.system.dao.UpmsPermissionMapper;
import com.shiyanlou.upms.system.domain.UpmsPermission;
import com.shiyanlou.upms.system.service.UpmsPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
// 新引入的包
import java.util.Set;
import java.util.List;
import java.util.Map;

/**
 * 资源管理
 */
@Service("upmsPermissionService")
public class UpmsPermissionServiceImpl implements UpmsPermissionService {

    @Autowired
    private UpmsPermissionMapper upmsPermissionMapper;

    @Override
    public int deleteByPrimaryKey(Integer permissionId) {
        return upmsPermissionMapper.deleteByPrimaryKey(permissionId);
    }

    @Override
    public int insert(UpmsPermission record) {
        return upmsPermissionMapper.insert(record);
    }

    @Override
    public int insertSelective(UpmsPermission record) {
        return upmsPermissionMapper.insertSelective(record);
    }

    @Override
    public UpmsPermission selectByPrimaryKey(Integer permissionId) {
        return upmsPermissionMapper.selectByPrimaryKey(permissionId);
    }

    @Override
    public int updateByPrimaryKeySelective(UpmsPermission record) {
        return upmsPermissionMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(UpmsPermission record) {
        return upmsPermissionMapper.updateByPrimaryKey(record);
    }

    @Override
    public int count(Map<String, Object> map) {
        return upmsPermissionMapper.count(map);
    }

    @Override
    public List<UpmsPermission> list(Map<String, Object> map) {
        return upmsPermissionMapper.list(map);
    }

    @Override
    public int batchRemove(Integer[] userIds) {
        return upmsPermissionMapper.batchRemove(userIds);
    }

    // 通过用户ID获取目录菜单信息
    @Override
    public List<UpmsPermission> selectPermissionByUserId(Integer userId) {
        return upmsPermissionMapper.selectPermissionByUserId(userId);
    }

    // 获取树形菜单
    @Override
    public List<Tree<UpmsPermission>> getTreeByUserId(Integer userId) {
        List<Tree<UpmsPermission>> trees = new ArrayList<Tree<UpmsPermission>>();
        List<UpmsPermission> upmsPermissionDOSet = upmsPermissionMapper.selectPermissionByUserId(userId);
        for (UpmsPermission permission : upmsPermissionDOSet) {
            Tree<UpmsPermission> tree = new Tree<UpmsPermission>();
            tree.setId(permission.getPermissionId().toString());
            tree.setText(permission.getName());
            tree.setParentId(permission.getPid().toString());
            Map<String, Object> attributes = new HashMap<>(16);
            attributes.put("url", permission.getUri());
            attributes.put("icon", permission.getIcon());
            tree.setAttributes(attributes);
            trees.add(tree);
        }
        List<Tree<UpmsPermission>> list = BuildTree.buildList(trees, "0");
        return list;
    }

    /**
     * 通过权限查找资源
     *
     * @param roleId
     * @return
     */
    @Override
    public List<UpmsPermission> selectPermissionByRoleId(Integer roleId) {
        return upmsPermissionMapper.selectPermissionByRoleId(roleId);
    }

    /**
     * 通过角色获取资源树
     *
     * @return
     */
    @Override
    public List<Tree<UpmsPermission>> getTree(Integer roleId) {
        List<Tree<UpmsPermission>> trees = new ArrayList<Tree<UpmsPermission>>();
        List<UpmsPermission> upmsPermissionList = selectPermissionByRoleId(roleId);
        for (UpmsPermission permissionDO : upmsPermissionList) {
            Tree<UpmsPermission> tree = new Tree<UpmsPermission>();
            tree.setId(permissionDO.getPermissionId().toString());
            tree.setText(permissionDO.getName());
            tree.setParentId(permissionDO.getPid().toString());
            Map<String, Object> state = new HashMap<String, Object>();
            state.put("opened", true);
            tree.setState(state);
            trees.add(tree);
        }
        List<Tree<UpmsPermission>> list = BuildTree.buildList(trees, "0");
        return list;
    }
    //通过用户 ID 查询资源信息
    @Override
    public Set<String> selectByUserId(Integer userId) {
        return upmsPermissionMapper.selectByUserId(userId);
    }
}
